package com.menhai.personmanagement.services;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.menhai.personmanagement.entity.ReportHealthcheck;
import com.menhai.personmanagement.entity.ReportHealthcheckHos;
import com.menhai.personmanagement.entity.ReportHealthcheckMed;
import com.menhai.personmanagement.entity.ReportHealthcheckVaccine;
import com.menhai.personmanagement.entity.vo.HealthCheckVo;
import com.menhai.personmanagement.mapper.HealthCheckMapper;
import com.menhai.personmanagement.mapper.ReportHealthcheckHosMapper;
import com.menhai.personmanagement.mapper.ReportHealthcheckMedMapper;
import com.menhai.personmanagement.mapper.ReportHealthcheckVaccineMapper;
/**
 * <p>
 * 报卡管理表 服务类
 * </p>
 *
 * @author shikun
 * @since 2021-11-25
 */
 @Service
public class HealthCheckService{

  
   @Autowired
   private HealthCheckMapper healthCheckMapper;
   @Autowired
   private ReportHealthcheckHosMapper reportHealthcheckHosMapper;
   @Autowired
   private ReportHealthcheckMedMapper reportHealthcheckMedMapper;
   @Autowired
   private ReportHealthcheckVaccineMapper reportHealthcheckVaccineMapper;
   
   
   /**
  	 * 新增慢病报卡
  	 * @param Doctor
  	 * @return Map<String, Object>
  	 */
  	@Transactional
  	public Map<String, Object> addReportHealthcheck(ReportHealthcheck reportHealthcheck) {
  		Map<String,Object> resultMap = new HashMap<String,Object>();
  		try{	
  			//新建健康体检表
  			int iRet = healthCheckMapper.insert(reportHealthcheck);
  			//新建住院治疗情况
  			String hosStr = reportHealthcheck.getHosStr().replace("undefined", "").replace("null", "");
  			if(!"".equals(hosStr)){
  				String[] splitArray = hosStr.split("\\|");
  	  			for(int i=0;i<splitArray.length;i++){
  	  				ReportHealthcheckHos reportHealthcheckHos = new ReportHealthcheckHos();
  	  				reportHealthcheckHos.setInHospital(splitArray[i].split(",")[0]);
  	  				reportHealthcheckHos.setOutHospital(splitArray[i].split(",")[1]);
  	  				reportHealthcheckHos.setWhy(splitArray[i].split(",")[2]);
  	  				reportHealthcheckHos.setHosName(splitArray[i].split(",")[3]);
  	  				reportHealthcheckHos.setMedNo(splitArray[i].split(",")[4]);
  	  				reportHealthcheckHos.setType(splitArray[i].split(",")[5]);
  	  				reportHealthcheckHos.setHealthCheckNumber(reportHealthcheck.getHealthCheckNumber());
  	  				reportHealthcheckHosMapper.insert(reportHealthcheckHos);
  	  			}
  			}
  			
  			//新建主要用药情况
  			String medStr = reportHealthcheck.getMedStr().replace("undefined", "").replace("null", "");
			if(!"".equals(medStr)){
				String[] splitArray2 = medStr.split("\\|");
	  			for(int i=0;i<splitArray2.length;i++){
	  				ReportHealthcheckMed reportHealthcheckMed = new ReportHealthcheckMed();
	  				reportHealthcheckMed.setMedName(splitArray2[i].split(",")[0]);
	  				reportHealthcheckMed.setUseWay(splitArray2[i].split(",")[1]);
	  				reportHealthcheckMed.setUseNum(splitArray2[i].split(",")[2]);
	  				reportHealthcheckMed.setUseTime(splitArray2[i].split(",")[3]);
	  				reportHealthcheckMed.setFyycxRadio(splitArray2[i].split(",")[4]);
	  				reportHealthcheckMed.setHealthCheckNumber(reportHealthcheck.getHealthCheckNumber());
	  				reportHealthcheckMedMapper.insert(reportHealthcheckMed);
	  			}
  			}
  			
  			//新建非免疫规划预防接种
  			String vaccineStr = reportHealthcheck.getVaccineStr().replace("undefined", "").replace("null", "");
  			if(!"".equals(vaccineStr)){
  				String[] splitArray3 = vaccineStr.split("\\|");
  	  			for(int i=0;i<splitArray3.length;i++){
  	  				ReportHealthcheckVaccine reportHealthcheckVaccine = new ReportHealthcheckVaccine();
  	  				reportHealthcheckVaccine.setVaccineName(splitArray3[i].split(",")[0]);
  	  				reportHealthcheckVaccine.setVaccineTime(splitArray3[i].split(",")[1]);
  	  				reportHealthcheckVaccine.setVaccinehos(splitArray3[i].split(",")[2]);
  	  				reportHealthcheckVaccine.setHealthCheckNumber(reportHealthcheck.getHealthCheckNumber());
  	  				reportHealthcheckVaccineMapper.insert(reportHealthcheckVaccine);
  	  			}
  			}
  			
  			
  			if(iRet == 0){  //没有插入成功的数据
  				resultMap.put("success", "0");
  				resultMap.put("message", "插入成功的条数为0，操作健康体检表失败！");
  				return resultMap;
  			}else{
  				resultMap.put("success", "1");
  				resultMap.put("message", "操作健康体检表成功！");
  				return resultMap;
  			}
  		}catch(Exception e){
  			resultMap.put("success", "0");
  			resultMap.put("message", "操作健康体检表失败！异常：" +e.getMessage());
  			e.printStackTrace();
  			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
  		}
  		return resultMap;
  	}

  	
  	/**
  	 * 查询健康体检信息列表
  	 * @param healthCheckVo
  	 * @return
  	 */
  	public IPage<HealthCheckVo> getHealthCheckInfoList(HealthCheckVo healthCheckVo){
        Page<HealthCheckVo> page = new Page<>(healthCheckVo.getPageIndex(),healthCheckVo.getPageSize());
        return healthCheckMapper.getHealthCheckInfoList(page,healthCheckVo);
    }
  	
  	/**
  	 * 根据体检id查询信息
  	 * @param inspectphysicalId
  	 * @return
  	 */
  	public ReportHealthcheck getOneByInspectphysicalId(String inspectphysicalId){
  		ReportHealthcheck oneByInspectphysicalId = healthCheckMapper.getOneByInspectphysicalId(inspectphysicalId);
  		//住院治疗情况-住院史
  		List<ReportHealthcheckHos> zysHosList = reportHealthcheckHosMapper.getHosList(oneByInspectphysicalId.getHealthCheckNumber(),"1");
  		String zyshosStr="";
  		if(zysHosList.size()>0){
  			for(int i=0;i<zysHosList.size();i++){
  				ReportHealthcheckHos reportHealthcheckHos = zysHosList.get(i);
  				zyshosStr = zyshosStr+reportHealthcheckHos.getId()+","+reportHealthcheckHos.getHealthCheckNumber()
  						 +","+reportHealthcheckHos.getInHospital()+","+reportHealthcheckHos.getOutHospital()
  						 +","+reportHealthcheckHos.getWhy()+","+reportHealthcheckHos.getHosName()+","+reportHealthcheckHos.getMedNo()
  						 +","+reportHealthcheckHos.getType()+"|";
  	  		}
  		}
  		if(!"".equals(zyshosStr)){
  			zyshosStr = zyshosStr.substring(0, zyshosStr.length()-1);
  		}
  		oneByInspectphysicalId.setZysHosStr(zyshosStr);
  		//住院治疗情况-家庭病床史
  		String jtbcHosStr="";
  		List<ReportHealthcheckHos> jtbcHosList = reportHealthcheckHosMapper.getHosList(oneByInspectphysicalId.getHealthCheckNumber(),"2");
  		if(jtbcHosList.size()>0){
  			for(int i=0;i<jtbcHosList.size();i++){
  				ReportHealthcheckHos reportHealthcheckHos = jtbcHosList.get(i);
  				jtbcHosStr = jtbcHosStr+reportHealthcheckHos.getId()+","+reportHealthcheckHos.getHealthCheckNumber()
  						 +","+reportHealthcheckHos.getInHospital()+","+reportHealthcheckHos.getOutHospital()
  						 +","+reportHealthcheckHos.getWhy()+","+reportHealthcheckHos.getHosName()+","+reportHealthcheckHos.getMedNo()
  						 +","+reportHealthcheckHos.getType()+"|";
  	  		}
  		}
  		if(!"".equals(jtbcHosStr)){
  			jtbcHosStr = jtbcHosStr.substring(0, jtbcHosStr.length()-1);
  		}
  		oneByInspectphysicalId.setJtbcHosStr(jtbcHosStr);
  		//主要用药情况
  		String medStr="";
  		List<ReportHealthcheckMed> healthCheckMedList = reportHealthcheckMedMapper.getHealthCheckMedList(oneByInspectphysicalId.getHealthCheckNumber());
  		if(healthCheckMedList.size()>0){
  			for(int i=0;i<healthCheckMedList.size();i++){
  				ReportHealthcheckMed reportHealthcheckMed = healthCheckMedList.get(i);
  				medStr = medStr+reportHealthcheckMed.getId()+","+reportHealthcheckMed.getMedName()+","+reportHealthcheckMed.getUseWay()
  						+","+reportHealthcheckMed.getUseNum()+","+reportHealthcheckMed.getUseTime()+","+reportHealthcheckMed.getFyycxRadio()
  						+", ,|";
  				
  			}
  		}
  		if(!"".equals(medStr)){
  			medStr = medStr.substring(0, medStr.length()-1);
  		}
  		oneByInspectphysicalId.setMedStr(medStr);
  		//非免疫规划预防接种
  		String vaccineStr="";
  		List<ReportHealthcheckVaccine> healthCheckVaccineList = reportHealthcheckVaccineMapper.getHealthCheckVaccineList(oneByInspectphysicalId.getHealthCheckNumber());
  		if(healthCheckVaccineList.size()>0){
  			for(int i=0;i<healthCheckVaccineList.size();i++){
  				ReportHealthcheckVaccine reportHealthcheckVaccine = healthCheckVaccineList.get(i);
  				vaccineStr = vaccineStr+reportHealthcheckVaccine.getId()+","+reportHealthcheckVaccine.getVaccineName()
  							+","+reportHealthcheckVaccine.getVaccineTime()+","+reportHealthcheckVaccine.getVaccinehos()+", ,|";
  			}
  		}
  		oneByInspectphysicalId.setVaccineStr(vaccineStr);
  		
  		return oneByInspectphysicalId;
  	}
  	
  	/**
  	 * 编辑信息
  	 * @param reportHealthcheck
  	 * @return
  	 */
	@Transactional
	public Map<String, Object> updateHealthCheck(ReportHealthcheck reportHealthcheck) {
		Map<String,Object> resultMap = new HashMap<String,Object>();
		try{	
			//编辑主表
			int iRet = healthCheckMapper.update(reportHealthcheck);
			//删除副表所有数据
			reportHealthcheckHosMapper.deleteByHealthCheckNumber(reportHealthcheck.getHealthCheckNumber());
			reportHealthcheckMedMapper.deleteByHealthCheckNumber(reportHealthcheck.getHealthCheckNumber());
			reportHealthcheckVaccineMapper.deleteByHealthCheckNumber(reportHealthcheck.getHealthCheckNumber());
			//重新插入
			//住院治疗情况
  			String hosStr = reportHealthcheck.getHosStr().replace("undefined", "").replace("null", "");
  			if(!"".equals(hosStr)){
  				String[] splitArray = hosStr.split("\\|");
  	  			for(int i=0;i<splitArray.length;i++){
  	  				ReportHealthcheckHos reportHealthcheckHos = new ReportHealthcheckHos();
  	  				reportHealthcheckHos.setInHospital(splitArray[i].split(",")[0]);
  	  				reportHealthcheckHos.setOutHospital(splitArray[i].split(",")[1]);
  	  				reportHealthcheckHos.setWhy(splitArray[i].split(",")[2]);
  	  				reportHealthcheckHos.setHosName(splitArray[i].split(",")[3]);
  	  				reportHealthcheckHos.setMedNo(splitArray[i].split(",")[4]);
  	  				reportHealthcheckHos.setType(splitArray[i].split(",")[5]);
  	  				reportHealthcheckHos.setHealthCheckNumber(reportHealthcheck.getHealthCheckNumber());
  	  				reportHealthcheckHosMapper.insert(reportHealthcheckHos);
  	  			}
  			}
  			//主要用药情况
  			String medStr = reportHealthcheck.getMedStr().replace("undefined", "").replace("null", "");
			if(!"".equals(medStr)){
				String[] splitArray2 = medStr.split("\\|");
	  			for(int i=0;i<splitArray2.length;i++){
	  				ReportHealthcheckMed reportHealthcheckMed = new ReportHealthcheckMed();
	  				reportHealthcheckMed.setMedName(splitArray2[i].split(",")[0]);
	  				reportHealthcheckMed.setUseWay(splitArray2[i].split(",")[1]);
	  				reportHealthcheckMed.setUseNum(splitArray2[i].split(",")[2]);
	  				reportHealthcheckMed.setUseTime(splitArray2[i].split(",")[3]);
	  				reportHealthcheckMed.setFyycxRadio(splitArray2[i].split(",")[4]);
	  				reportHealthcheckMed.setHealthCheckNumber(reportHealthcheck.getHealthCheckNumber());
	  				reportHealthcheckMedMapper.insert(reportHealthcheckMed);
	  			}
  			}
			//非免疫规划预防接种
			String vaccineStr = reportHealthcheck.getVaccineStr().replace("undefined", "").replace("null", "");
  			if(!"".equals(vaccineStr)){
  				String[] splitArray3 = vaccineStr.split("\\|");
  	  			for(int i=0;i<splitArray3.length;i++){
  	  				ReportHealthcheckVaccine reportHealthcheckVaccine = new ReportHealthcheckVaccine();
  	  				reportHealthcheckVaccine.setVaccineName(splitArray3[i].split(",")[0]);
  	  				reportHealthcheckVaccine.setVaccineTime(splitArray3[i].split(",")[1]);
  	  				reportHealthcheckVaccine.setVaccinehos(splitArray3[i].split(",")[2]);
  	  				reportHealthcheckVaccine.setHealthCheckNumber(reportHealthcheck.getHealthCheckNumber());
  	  				reportHealthcheckVaccineMapper.insert(reportHealthcheckVaccine);
  	  			}
  			}
			if(iRet == 0){  
				resultMap.put("success", "0");
				resultMap.put("message", "编辑成功的条数为0，编辑失败！");
				return resultMap;
			}
			resultMap.put("success", "1");
			resultMap.put("message", "编辑成功！");
		}catch(Exception e){
			resultMap.put("success", "0");
			resultMap.put("message", "编辑失败！异常：" +e.getMessage());
			e.printStackTrace();
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
		}
		return resultMap;
	}
	
	/**
	 * 获取责任医生列表
	 * @param hosnum
	 * @return
	 */
	public List<Map<String,Object>> getZedocList(String hosnum){
		return healthCheckMapper.getZedocList(hosnum);
	}
	
	/**
	 * 获取导出信息
	 * @param id
	 * @return
	 */
	public Map<String,String> getExportInfo(@Param("id")String id){
		return healthCheckMapper.getExportInfo(id);
	}
	
	
	/**
	 * 获取导出住院信息
	 * @param type
	 * @return
	 */
	public List<Map<String,Object>> getExportHosInfo(String type,String id){
		return healthCheckMapper.getExportHosInfo(type,id);
	}
	
	/**
	 * 获取导出用药信息
	 * @param type
	 * @return
	 */
	public List<Map<String,Object>> getExportMedInfo(String id){
		return healthCheckMapper.getExportMedInfo(id);
	}
	
	
	/**
	 * 获取导出疫苗信息
	 * @param type
	 * @return
	 */
	public List<Map<String,Object>> getExportVaccineInfo(String id){
		return healthCheckMapper.getExportVaccineInfo(id);
	}
	
	
	/**
	 * 左侧树
	 * @param hosnum
	 * @return
	 */
	public List<Map<String,Object>> geteCunRenShu(String hosnum){
		return healthCheckMapper.geteCunRenShu(hosnum);
	}
	
}
